<template>
  <div>
    <div class="operation" style="display: flex">
      <div style="margin:10px 10px">
        <el-input style="width: 200px" placeholder="请输入标题" suffix-icon="el-icon-search" v-model="title"></el-input>
        <el-input style="width: 200px" placeholder="请输入内容" suffix-icon="el-icon-search" class="ml-5"
                  v-model="content"></el-input>

        <el-button class=ml-5 type="primary" @click="load">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </div>
      <div style="margin: 10px 0">
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon="el-icon-info"
            icon-color="red"
            title="您确定批量删除这些数据吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
    </div>

    <el-table :data="allpostList" border stripe header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="id" width="100" align="center"></el-table-column>
      <el-table-column prop="uid" label="用户昵称" width="140" align="center">
        <template slot-scope="scope">
          {{ scope.row.user.nickname }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="120" align="center"></el-table-column>
      <el-table-column prop="content" label="内容" align="center"></el-table-column>
      <!--            <el-table-column prop="phone" label="电话" width="160"></el-table-column>
                  <el-table-column prop="avatar" label="头像" width="77" align="center">
                      <template slot-scope="scope">
                          <el-avatar  shape="circle" :size="50" :src="scope.row.avatar"></el-avatar>
                          　　    </template>
                  </el-table-column>-->
      <el-table-column label="操作" width="115" align="center">
        <template slot-scope="scope">
          <!--                <el-button type="success" @click="handleEdit(scope.row)">编辑<i class="el-icon-edit"></i></el-button>-->
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
      <!--<div v-for="item in usernameList">
          {{item.nickname}}
      </div>-->
    </div>
  </div>
</template>

<script>
import axios from "axios";
import echarts from 'echarts'

export default {
  name: "Post",
  data() {
    return {
      uid: "",
      title: "",
      content: "",
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      username: "",
      nickname: "",
      email: "",
      avatar: "",
      circleUrl: "",
      dialogFormVisible: false,
      multipleSelection: [],
      form: {},
      headerBg: 'headerBg',
      allpostList: [],
    }
  },
  created() {
    this.load();
    /*this.getData();*/
  },
  methods: {
    getData() {
      axios({
        method: "GET",
        url: this.$e.Url + "/user/getAllHotPosts",
      }).then((res) => {
        console.log("请求API接口【/user/getAllHotPosts】", res);
        this.allpostList = res.data;
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val

    },
    del(id) {
      axios({
        url: this.$e.Url + "/admin/checkToken",
        params: {
          Token: localStorage.getItem("Token")|| ""
        },
        method: "GET"
      }).then(ans => {
        console.log("请求API接口【/admin/checkToken】", ans);
        if (ans.data.result) {
          axios({
            method: "DELETE",
            url: this.$e.Url + "/user/deletePost",
            data: id
          }).then((res) => {
            console.log("请求API接口【/user/deletePost】", res);
            console.log("-----------------" + this.$e.Url + "/user/deletePost");
            if (res) {
              this.$message.success("删除成功")
            } else {
              this.$message.error("删除失败")
            }
            this.load();
          })
        } else {
          this.$message.error("身份过期,权限不够");
        }
      })


    },
    load() {
      axios.get(this.$e.Url + "/admin/FindPost", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              title: this.title,
              content: this.content
            }
          }
      ).then(res => {
        console.log("请求API接口【/admin/FindPost】", res)
        this.allpostList = res.data.data.sort((a, b) => b.time - a.time);
        this.total = res.data.total
        /* this.usernameList=res.data;*/
      })
      //已有request不用fetch

      /*fetch("http://localhost:9090/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize +"&username=" + this.username)
              .then(res =>res.json()).then(res=>{
        console.log(res)
        this.tableData=res.data
        this.total=res.total
      })*/
    },
    reset() {
      this.title = ""
      this.content = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    delBatch() {
      axios({
        url: this.$e.Url + "/admin/checkToken",
        params: {
          Token: localStorage.getItem("Token")|| ""
        },
        method: "GET"
      }).then(ans => {
        console.log("请求API接口【/admin/checkToken】", ans);
        if (ans.data.result) {
          let ids = this.multipleSelection.map(v => v.id)  // 获取所有需要删除的id
          let deletePromises = ids.map(id => {  // 将每个删除操作转换为一个Promise
            return axios.delete(this.$e.Url + "/user/deletePost", {
              data: {
                id: id,
              }
            });
          });
          console.log("删除信息", deletePromises)
          Promise.all(deletePromises).then(responses => {  // 等待所有删除操作完成
            // 检查所有删除操作的响应，如果有任何一个响应表示删除失败，则认为批量删除失败
            let success = responses.every(response => response.status === 200);
            if (success) {
              this.$message.success("批量删除成功");
            } else {
              this.$message.error("批量删除失败");
            }
            this.load();
          });
        } else {
          this.$message.error("身份过期,权限不够");
        }
      })


    },
  }
}
</script>

<style scoped>

</style>
